const Koa = require('koa')
const Router = require('koa-router')
const app = new Koa()
const router = new Router()
const ytdl = require('ytdl-core')
const tpl = require('./tpl')

router.get('/', async(ctx) => {
  ctx.type = 'text/html; charset=utf-8'
  ctx.body = tpl
})

router.get('/download', async(ctx) => {
  const { url } = ctx.query
  const stream = ytdl(url, { 
    quality: ['18'], 
    filter: format => format.container === 'mp4' 
  })
  ctx.body = stream
})

app.use(router.routes())

app.listen(3000)
console.log('App start!')